package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.dao.AppUserSearchHistoryMapper;
import com.eco.fanliapp.entity.AppUserSearchHistory;
import com.eco.fanliapp.service.IAppUserSearchHistoryService;
import com.eco.fanliapp.service.RedisLockService;
import com.eco.fanliapp.service.RedisTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class AppUserSearchHistoryServiceImpl extends ServiceImpl<AppUserSearchHistoryMapper, AppUserSearchHistory> implements IAppUserSearchHistoryService {

    @Autowired
    private AppUserSearchHistoryMapper appUserSearchHistoryMapper;
    @Autowired
    private RedisTemplateService redisTemplateService;
    @Autowired
    private RedisLockService redisLockService;

    @Override
    public AppUserSearchHistory selectByUserId(Long userId) {
        return appUserSearchHistoryMapper.selectByUserId(userId);
    }

    @Override
    public int selectCountSearchCouponPeople(Integer companyId) {
        return appUserSearchHistoryMapper.selectCountSearchCouponPeople(companyId);
    }

    @Override
    public int yesterdaySelectCountSearchCouponPeople(int flag, Integer companyId, long startTime, long endTime) {
        return appUserSearchHistoryMapper.yesterdaySelectCountSearchCouponPeople(flag, companyId, startTime, endTime);
    }

    @Override
    @Async
    public void saveHistory(long seconds) {
        try {
            if ( !redisLockService.lock("insertUserSearchHistory_inDatabase_" + seconds, (System.currentTimeMillis() + 60000L) + "")) {
                return ;
            }

            Thread.sleep(60 * 1000);

            String redisKeyStr = "AppUserSearchHistory_" + seconds;
            int i = 0;
            int count = 0;
            if (redisTemplateService.hasKey(redisKeyStr)) {
                Map<String, String> searchMap = redisTemplateService.getMap(redisKeyStr, Map.class);
                List<AppUserSearchHistory> list = new ArrayList<>();
                for (String itemkey : searchMap.keySet()) {
                    QueryWrapper<AppUserSearchHistory> searchHistoryQueryWrapper = new QueryWrapper<AppUserSearchHistory>();
                    searchHistoryQueryWrapper.eq("user_id", itemkey).eq("item_id", searchMap.get(itemkey));
                    count = appUserSearchHistoryMapper.selectCount(searchHistoryQueryWrapper);
                    AppUserSearchHistory appUserSearchHistory = new AppUserSearchHistory();
                    if (count <= 0) {
                        appUserSearchHistory.setUserId(Long.valueOf(itemkey));
                        appUserSearchHistory.setSearchCount(1);
                        appUserSearchHistory.setItemId(Long.parseLong(searchMap.get(itemkey)));
                        list.add(appUserSearchHistory);
                    }
                }
                if (list.size() > 0) {
                    i = appUserSearchHistoryMapper.insertSearchHistoryList(list);
                }
                //redis
                if (i > 0 || count > 0) {
                    redisTemplateService.del("AppUserSearchHistory_" + seconds);
                }
            }
        } catch (Exception e) {

            e.printStackTrace();
        }


    }

    @Override
    public int selectCountSearchCouponPeopleNew(Integer flag, Integer companyId, long startTime, long endTime) {
        return appUserSearchHistoryMapper.selectCountSearchCouponPeopleNew(flag,companyId,startTime,endTime);
    }


}
